<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>

<%@ include file="../bui_header.jsp" %>
<body>

<div class="container">
    <div class="row">
      <form id="searchForm" class="form-horizontal span24">
        
        <div class="row">
          <div class="control-group span8">
            <label class="control-label">公司名称：</label>
            <div class="controls">
              <input type="text" class="text" name="company_name">
            </div>
          </div>
          <div class="span4">
            <button type="button" id="btnSearch" class="button button-primary">搜索</button>
          </div>
        </div>
      </form>
    </div>
    <div class="search-grid-container">
      <div id="grid"></div>
    </div>
  </div>
    
  <div id="content" class="hide" disabled='true'>
      <form id="user_form" class="form-horizontal">
        <div class="row">
			<div class="control-group span8">
				<label class="control-label">请选择公司</label>
				<div class="controls">
					<div id="companySelect"></div>
				</div>
		  	</div>
          <!-- <div class="control-group span8">
            <label class="control-label"><s>*</s>企业名称</label>
            <div class="controls">
              <input name="companyName" type="text" data-rules="{required:true}" class="input-normal control-text">
            </div>
          </div> -->
          <input name="companyName" type="hidden" data-rules="{required:true}" class="input-normal control-text">
          <div class="control-group span8">
            <label class="control-label"><s>*</s>所属年度</label>
            <div class="controls">
              <input name="belongYear" type="text" data-rules="{required:true}" class="input-normal control-text">
            </div>
          </div>
        </div>
        <div class="row">
          <div class="control-group span8">
            <label class="control-label"><s>*</s>获奖名称</label>
            <div class="controls">
              <input name="bearName" type="text" data-rules="{required:true}" class="input-normal control-text">
            </div>
          </div>
          <div class="control-group span8">
            <label class="control-label"><s>*</s>颁奖单位</label>
            <div class="controls">
              <input name=prizeUnit type="text" data-rules="{required:true}" class="input-normal control-text">
            </div>
          </div>
        </div>
        <div class="row">
          <div class="control-group span8">
            <label class="control-label"><s>*</s>获奖时间</label>
            <div class="controls">
              <input name="bearDate" type="text" data-rules="{required:true}" class="calendar">
            </div>
          </div>
          <div class="control-group span8">
            <label class="control-label"><s>*</s>获奖评分</label>
            <div class="controls">
              <input name="bearGrade" type="text" data-rules="{required:true}" class="input-normal control-text">
            </div>
          </div>
        </div>
      </form>
    </div>
    <div class="container hide">
	    <div class="row">
	    <div class="span24">
	      <div id="importFileDiv">
	      </div>
	    </div>
	 </div>
	 </div>
  
<script type="text/javascript">
BUI.use(['common/search', 'bui/overlay', 'bui/uploader', 'bui/form', 'bui/select', 'bui/data'], function (Search, Overlay, Uploader, Form, Select, Data) {
    
    var editing = new BUI.Grid.Plugins.DialogEditing({
        contentId : 'content', //设置隐藏的Dialog内容
        autoSave : true,//添加数据或者修改数据时，自动保存
      });
    
    var columns = [
	  /* {title:'编号',dataIndex:'number',width:'15%'
	  }, */
	  {title:'企业名称', dataIndex:'companyName', width:'15%'},
	  {title:'所属年度', dataIndex:'belongYear',width:'10%'},
	  {title:'获奖名称',dataIndex:'bearName',width:'15%'},
	  {title:'颁奖单位',dataIndex:'prizeUnit',width:'15%'}, 
	  {title:'获奖时间',dataIndex:'bearDate',width:'15%'}, 
	  {title:'获奖评分',dataIndex:'bearGrade',width:'10%'}, 
	  {title: '操作', width: '20%', renderer: function(value, obj) {
		  var viewBtn = '<span class="grid-command btn-view">查看</span>&nbsp;&nbsp;';
		  var editBtn = '<span class="grid-command btn-edit">编辑</span>';
		  return viewBtn + editBtn;
	  }}
	];
	var store = Search.createStore('<%=contextPath %>/bizinfo/hjxx/getdata.do', {
		  autoSync: true,
		  proxy: {
	  		method: "post",
	  		dataType : 'json',
	  		save : { //也可以是一个字符串，那么增删改，都会往那么路径提交数据，同时附加参数saveType
				addUrl : '<%=contextPath %>/bizinfo/hjxx/editData.do?action=add',
				updateUrl : '<%=contextPath %>/bizinfo/hjxx/editData.do?action=update',
				removeUrl : '<%=contextPath %>/bizinfo/hjxx/editData.do?action=remove'
	   		}
	 }
	});
	initSearchStore(store);
     
	var height = $(window).height() - 70;      
      var gridCfg = Search.createGridCfg(columns, {
    	  //width:'200%',
    	  height: height,
        tbar : {
          items : [
            {text : '<i class="icon-plus"></i>新建',btnCls : 'button button-small',handler: addFunction},
            {text : '<i class="icon-remove"></i>删除',btnCls : 'button button-small',handler : delFunction},
            {text : '<i class="icon-upload icon-white"></i>导入',btnCls : 'button button-small button-primary', handler: importData}
          ]
        },
        plugins : [BUI.Grid.Plugins.CheckSelection,editing] // 插件形式引入多选表格
      });

    var search = new Search({
        store : store,
        gridCfg : gridCfg
      });
    var grid = search.get('grid');
    
    initEditing(editing);
    
	ss();
    
    function ss() {
    	var $grid = $(".bui-grid");
    	if (!$grid)
  		  return ;
    	var $gridBody = $grid.find(".bui-grid-body");
    	if (!$gridBody)
  		  return ;
    	$gridBody.css('overflow-y', 'auto');
	  
	  var $gridTable = $grid.find(".bui-grid-table");
	  if ($gridTable)
		  $gridTable.width($gridBody.width()); 
    }
    
    // 新增操作
    function addFunction(){
        var newData = {isNew : true}; //标志是新增加的记录
        editing.add(newData); //添加记录后，直接编辑
        setCompanySelectVisible(true);
    }
    
    // 删除操作
    function delFunction(){
      var selections = grid.getSelection();
      delItems(selections);
    }
    function importData() {
    	var uploader = new Uploader.Uploader({
   	      	render: '#importFileDiv',
   	      	url: '<%=contextPath %>/bizinfo/hjxx/importData.do',
   	      	autoUpload: false,
   	      	multiple: false,
   	      	text: '选择导入文件',
   	     	width: 360,
   	     	rules: {
   	     	 // maxSize: [1024, '文件最大不能超过1M!'],
   	     	 // minSize: [1, '文件最小不能小于1k!'],
   	     	  max: [1, '文件最多不能超过{0}个！'],
   	     	 // min: [1, '文件最少不能少于{0}个!'],
   	     	  ext: ['.xls', '文件类型只能为{0}']
   	     	},
   	     	isSuccess: function(result) {
   	     		if (result && result.success) {
		   	     	BUI.Message.Alert('导入数据成功！', function(){
		   	     		importDialog.close();
		   	     		search.load();
		   	        }, 'success');
   	     		} else {
   	     			BUI.Message.Alert(result.error, function(){
		   	          //alert('确认');
		   	        }, 'error');
   	     		}
   	     	}
   	    }).render();
    	 
    	var importDialog = new Overlay.Dialog({
            title: '导入文件',
            width: 400,
            height: 200,
            contentId: 'importFileDiv',
            success:function () {
              //alert('确认');
              //this.close();
              uploader.upload();
            }
          });
    	importDialog.show();
    }


    function delItems(items){
        var ids = [];
        BUI.each(items,function(item){
          ids.push(item.number);
        });

        if(ids.length){
          BUI.Message.Confirm('确认要删除选中的记录么？',function(){
          	$.post('<%=contextPath %>/bizinfo/hjxx/editData.do?action=remove', {ids: ids}, function(ret) {
          		if(ret.success){ //删除成功
                  	search.load();
                  	BUI.Message.Alert('删除成功！');
                  } else { //删除失败
                      BUI.Message.Alert('删除失败！');
                  }
          	}, 'json');
          },'question');
        }
      }

    //监听事件，删除一条记录
    grid.on('cellclick', function(ev) {
    	var sender = $(ev.domTarget);
       	var editor = editing.get('editor');
      	var contain = false;
		if(sender.hasClass('btn-edit') && editor){
			contain = true;
			editing.edit(ev.record);
			setCompanySelectVisible(true, ev.record);
		}
       	if (sender.hasClass('btn-view') && editor) {
       		contain = true;
     		var record = ev.record;
     		var view = {"view": true};
     		record.trigger_action = "view";
     		editing.showEditor(record);
     		setEditingEnable(editing, false);
     		setCompanySelectVisible(false, record);
     	}
       	if (contain && grid.get('multipleSelect')){
		    return false;
		}
   });

     var select = new Select.Suggest({  
        render:'#companySelect',
        //valueField:'#hide',
        multipleSelect : false,
        name: 'company_name',
        //store : selectStore,
        dataType : 'json',
        cacheable: true,
        url: '<%=contextPath %>/bizinfo/hjxx/selectCompany.do'
      });
      select.render();
      select.on('change', function(ev){
        //alert(ev.item.text);
        //$("input[name='number']").val(ev.item.value);
        $("input[name='companyName']").val(ev.item.text);
      });
      
      function setCompanySelectVisible(visible, record) {
    	  var $select = $("#companySelect");
    	  var $companyName = $("input[name='companyName']");
    	  if (visible) {
    		  //$select.show();
    		  $select.find("input[name='company_name']").removeAttr("disabled");
    		  if (record) {
    			  $select.find("input[name='company_name']").val(record.companyName);
    		  } else {
    		      $select.find("input[name='company_name']").val("");
    		  }
    		//  if ($companyName)
    			//  $companyName.removeAttr("disabled");
    	  } else {
    		  $select.find("input[name='company_name']").val(record.companyName);
    		  $select.find("input[name='company_name']").attr("disabled", "disabled");
    		  //if ($companyName)
    			//  $companyName.attr("disabled", "disabled");
    	  }
      }
      
});

</script>

		
</body>
</html>